import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token') || ''
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken (state, token) {
      state.token = token
      // localStorage.setItem('token', token)
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async getUserInfo (context) {
      // const { data: res } = await axios.get('/my/userinfo', {
      //   headers: {
      //     Authorization: context.state.token
      //   }
      // })
      const { data: res } = await axios.get('/my/userinfo')

      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    }
  },
  modules: {}
})
